<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>LCOV - code analysis - include/caffe/solver_factory.hpp</title>
  <link rel="stylesheet" type="text/css" href="../../gcov.css">
</head>

<body>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="title">LCOV - code coverage report</td></tr>
    <tr><td class="ruler"><img src="../../glass.png" width=3 height=3 alt=""></td></tr>

    <tr>
      <td width="100%">
        <table cellpadding=1 border=0 width="100%">
          <tr>
            <td width="10%" class="headerItem">Current view:</td>
            <td width="35%" class="headerValue"><a href="../../index.html">top level</a> - <a href="index.html">include/caffe</a> - solver_factory.hpp<span style="font-size: 80%;"> (source / <a href="solver_factory.hpp.func-sort-c.html">functions</a>)</span></td>
            <td width="5%"></td>
            <td width="15%"></td>
            <td width="10%" class="headerCovTableHead">Hit</td>
            <td width="10%" class="headerCovTableHead">Total</td>
            <td width="15%" class="headerCovTableHead">Coverage</td>
          </tr>
          <tr>
            <td class="headerItem">Test:</td>
            <td class="headerValue">code analysis</td>
            <td></td>
            <td class="headerItem">Lines:</td>
            <td class="headerCovTableEntry">9</td>
            <td class="headerCovTableEntry">24</td>
            <td class="headerCovTableEntryLo">37.5 %</td>
          </tr>
          <tr>
            <td class="headerItem">Date:</td>
            <td class="headerValue">2020-09-11 22:25:26</td>
            <td></td>
            <td class="headerItem">Functions:</td>
            <td class="headerCovTableEntry">4</td>
            <td class="headerCovTableEntry">7</td>
            <td class="headerCovTableEntryLo">57.1 %</td>
          </tr>
          <tr>
            <td class="headerItem">Legend:</td>
            <td class="headerValueLeg">            Lines:
            <span class="coverLegendCov">hit</span>
            <span class="coverLegendNoCov">not hit</span>
</td>
            <td></td>
          </tr>
          <tr><td><img src="../../glass.png" width=3 height=3 alt=""></td></tr>
        </table>
      </td>
    </tr>

    <tr><td class="ruler"><img src="../../glass.png" width=3 height=3 alt=""></td></tr>
  </table>

  <table cellpadding=0 cellspacing=0 border=0>
    <tr>
      <td><br></td>
    </tr>
    <tr>
      <td>
<pre class="sourceHeading">          Line data    Source code</pre>
<pre class="source">
<a name="1"><span class="lineNum">       1 </span>            : /**</a>
<span class="lineNum">       2 </span>            :  * @brief A solver factory that allows one to register solvers, similar to
<span class="lineNum">       3 </span>            :  * layer factory. During runtime, registered solvers could be called by passing
<span class="lineNum">       4 </span>            :  * a SolverParameter protobuffer to the CreateSolver function:
<span class="lineNum">       5 </span>            :  *
<span class="lineNum">       6 </span>            :  *     SolverRegistry&lt;Dtype&gt;::CreateSolver(param);
<span class="lineNum">       7 </span>            :  *
<span class="lineNum">       8 </span>            :  * There are two ways to register a solver. Assuming that we have a solver like:
<span class="lineNum">       9 </span>            :  *
<span class="lineNum">      10 </span>            :  *   template &lt;typename Dtype&gt;
<span class="lineNum">      11 </span>            :  *   class MyAwesomeSolver : public Solver&lt;Dtype&gt; {
<span class="lineNum">      12 </span>            :  *     // your implementations
<span class="lineNum">      13 </span>            :  *   };
<span class="lineNum">      14 </span>            :  *
<span class="lineNum">      15 </span>            :  * and its type is its C++ class name, but without the &quot;Solver&quot; at the end
<span class="lineNum">      16 </span>            :  * (&quot;MyAwesomeSolver&quot; -&gt; &quot;MyAwesome&quot;).
<span class="lineNum">      17 </span>            :  *
<span class="lineNum">      18 </span>            :  * If the solver is going to be created simply by its constructor, in your C++
<span class="lineNum">      19 </span>            :  * file, add the following line:
<span class="lineNum">      20 </span>            :  *
<span class="lineNum">      21 </span>            :  *    REGISTER_SOLVER_CLASS(MyAwesome);
<span class="lineNum">      22 </span>            :  *
<span class="lineNum">      23 </span>            :  * Or, if the solver is going to be created by another creator function, in the
<span class="lineNum">      24 </span>            :  * format of:
<span class="lineNum">      25 </span>            :  *
<span class="lineNum">      26 </span>            :  *    template &lt;typename Dtype&gt;
<span class="lineNum">      27 </span>            :  *    Solver&lt;Dtype*&gt; GetMyAwesomeSolver(const SolverParameter&amp; param) {
<span class="lineNum">      28 </span>            :  *      // your implementation
<span class="lineNum">      29 </span>            :  *    }
<span class="lineNum">      30 </span>            :  *
<span class="lineNum">      31 </span>            :  * then you can register the creator function instead, like
<span class="lineNum">      32 </span>            :  *
<span class="lineNum">      33 </span>            :  * REGISTER_SOLVER_CREATOR(MyAwesome, GetMyAwesomeSolver)
<span class="lineNum">      34 </span>            :  *
<span class="lineNum">      35 </span>            :  * Note that each solver type should only be registered once.
<span class="lineNum">      36 </span>            :  */
<span class="lineNum">      37 </span>            : 
<span class="lineNum">      38 </span>            : #ifndef CAFFE_SOLVER_FACTORY_H_
<span class="lineNum">      39 </span>            : #define CAFFE_SOLVER_FACTORY_H_
<span class="lineNum">      40 </span>            : 
<span class="lineNum">      41 </span>            : #include &lt;map&gt;
<span class="lineNum">      42 </span>            : #include &lt;string&gt;
<span class="lineNum">      43 </span>            : #include &lt;vector&gt;
<span class="lineNum">      44 </span>            : 
<span class="lineNum">      45 </span>            : #include &quot;caffe/common.hpp&quot;
<span class="lineNum">      46 </span>            : #include &quot;caffe/proto/caffe.pb.h&quot;
<span class="lineNum">      47 </span>            : 
<span class="lineNum">      48 </span>            : namespace caffe {
<span class="lineNum">      49 </span>            : 
<span class="lineNum">      50 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      51 </span>            : class Solver;
<span class="lineNum">      52 </span>            : 
<span class="lineNum">      53 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      54 </span>            : class SolverRegistry {
<span class="lineNum">      55 </span>            :  public:
<span class="lineNum">      56 </span>            :   typedef Solver&lt;Dtype&gt;* (*Creator)(const SolverParameter&amp;);
<a name="57"><span class="lineNum">      57 </span>            :   typedef std::map&lt;string, Creator&gt; CreatorRegistry;</a>
<span class="lineNum">      58 </span>            : 
<span class="lineNum">      59 </span><span class="lineCov">         12 :   static CreatorRegistry&amp; Registry() {</span>
<span class="lineNum">      60 </span><span class="lineCov">         14 :     static CreatorRegistry* g_registry_ = new CreatorRegistry();</span>
<span class="lineNum">      61 </span><span class="lineCov">         12 :     return *g_registry_;</span>
<span class="lineNum">      62 </span>            :   }
<a name="63"><span class="lineNum">      63 </span>            : </a>
<span class="lineNum">      64 </span>            :   // Adds a creator.
<span class="lineNum">      65 </span><span class="lineCov">         12 :   static void AddCreator(const string&amp; type, Creator creator) {</span>
<span class="lineNum">      66 </span><span class="lineCov">         12 :     CreatorRegistry&amp; registry = Registry();</span>
<span class="lineNum">      67 </span><span class="lineCov">         48 :     CHECK_EQ(registry.count(type), 0)</span>
<span class="lineNum">      68 </span>            :         &lt;&lt; &quot;Solver type &quot; &lt;&lt; type &lt;&lt; &quot; already registered.&quot;;
<span class="lineNum">      69 </span><span class="lineCov">         12 :     registry[type] = creator;</span>
<span class="lineNum">      70 </span><span class="lineCov">         12 :   }</span>
<span class="lineNum">      71 </span>            : 
<span class="lineNum">      72 </span>            :   // Get a solver using a SolverParameter.
<span class="lineNum">      73 </span><span class="lineNoCov">          0 :   static Solver&lt;Dtype&gt;* CreateSolver(const SolverParameter&amp; param) {</span>
<span class="lineNum">      74 </span>            :     const string&amp; type = param.type();
<span class="lineNum">      75 </span><span class="lineNoCov">          0 :     CreatorRegistry&amp; registry = Registry();</span>
<span class="lineNum">      76 </span><span class="lineNoCov">          0 :     CHECK_EQ(registry.count(type), 1) &lt;&lt; &quot;Unknown solver type: &quot; &lt;&lt; type</span>
<span class="lineNum">      77 </span><span class="lineNoCov">          0 :         &lt;&lt; &quot; (known types: &quot; &lt;&lt; SolverTypeListString() &lt;&lt; &quot;)&quot;;</span>
<span class="lineNum">      78 </span><span class="lineNoCov">          0 :     return registry[type](param);</span>
<a name="79"><span class="lineNum">      79 </span>            :   }</a>
<span class="lineNum">      80 </span>            : 
<span class="lineNum">      81 </span><span class="lineNoCov">          0 :   static vector&lt;string&gt; SolverTypeList() {</span>
<span class="lineNum">      82 </span><span class="lineNoCov">          0 :     CreatorRegistry&amp; registry = Registry();</span>
<span class="lineNum">      83 </span>            :     vector&lt;string&gt; solver_types;
<span class="lineNum">      84 </span><span class="lineNoCov">          0 :     for (typename CreatorRegistry::iterator iter = registry.begin();</span>
<span class="lineNum">      85 </span>            :          iter != registry.end(); ++iter) {
<span class="lineNum">      86 </span><span class="lineNoCov">          0 :       solver_types.push_back(iter-&gt;first);</span>
<span class="lineNum">      87 </span>            :     }
<span class="lineNum">      88 </span><span class="lineNoCov">          0 :     return solver_types;</span>
<span class="lineNum">      89 </span>            :   }
<span class="lineNum">      90 </span>            : 
<span class="lineNum">      91 </span>            :  private:
<span class="lineNum">      92 </span>            :   // Solver registry should never be instantiated - everything is done with its
<span class="lineNum">      93 </span>            :   // static variables.
<span class="lineNum">      94 </span>            :   SolverRegistry() {}
<span class="lineNum">      95 </span>            : 
<span class="lineNum">      96 </span><span class="lineNoCov">          0 :   static string SolverTypeListString() {</span>
<span class="lineNum">      97 </span><span class="lineNoCov">          0 :     vector&lt;string&gt; solver_types = SolverTypeList();</span>
<span class="lineNum">      98 </span>            :     string solver_types_str;
<span class="lineNum">      99 </span><span class="lineNoCov">          0 :     for (vector&lt;string&gt;::iterator iter = solver_types.begin();</span>
<span class="lineNum">     100 </span>            :          iter != solver_types.end(); ++iter) {
<span class="lineNum">     101 </span><span class="lineNoCov">          0 :       if (iter != solver_types.begin()) {</span>
<span class="lineNum">     102 </span>            :         solver_types_str += &quot;, &quot;;
<span class="lineNum">     103 </span>            :       }
<span class="lineNum">     104 </span>            :       solver_types_str += *iter;
<span class="lineNum">     105 </span>            :     }
<span class="lineNum">     106 </span><span class="lineNoCov">          0 :     return solver_types_str;</span>
<span class="lineNum">     107 </span>            :   }
<span class="lineNum">     108 </span>            : };
<span class="lineNum">     109 </span>            : 
<span class="lineNum">     110 </span>            : 
<span class="lineNum">     111 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">     112 </span>            : class SolverRegisterer {
<span class="lineNum">     113 </span>            :  public:
<span class="lineNum">     114 </span>            :   SolverRegisterer(const string&amp; type,
<span class="lineNum">     115 </span>            :       Solver&lt;Dtype&gt;* (*creator)(const SolverParameter&amp;)) {
<span class="lineNum">     116 </span>            :     // LOG(INFO) &lt;&lt; &quot;Registering solver type: &quot; &lt;&lt; type;
<span class="lineNum">     117 </span><span class="lineCov">         12 :     SolverRegistry&lt;Dtype&gt;::AddCreator(type, creator);</span>
<span class="lineNum">     118 </span>            :   }
<span class="lineNum">     119 </span>            : };
<span class="lineNum">     120 </span>            : 
<span class="lineNum">     121 </span>            : 
<span class="lineNum">     122 </span>            : #define REGISTER_SOLVER_CREATOR(type, creator)                                 \
<span class="lineNum">     123 </span>            :   static SolverRegisterer&lt;float&gt; g_creator_f_##type(#type, creator&lt;float&gt;);    \
<span class="lineNum">     124 </span>            :   static SolverRegisterer&lt;double&gt; g_creator_d_##type(#type, creator&lt;double&gt;)   \
<span class="lineNum">     125 </span>            : 
<span class="lineNum">     126 </span>            : #define REGISTER_SOLVER_CLASS(type)                                            \
<span class="lineNum">     127 </span>            :   template &lt;typename Dtype&gt;                                                    \
<span class="lineNum">     128 </span>            :   Solver&lt;Dtype&gt;* Creator_##type##Solver(                                       \
<span class="lineNum">     129 </span>            :       const SolverParameter&amp; param)                                            \
<span class="lineNum">     130 </span>            :   {                                                                            \
<span class="lineNum">     131 </span>            :     return new type##Solver&lt;Dtype&gt;(param);                                     \
<span class="lineNum">     132 </span>            :   }                                                                            \
<span class="lineNum">     133 </span>            :   REGISTER_SOLVER_CREATOR(type, Creator_##type##Solver)
<span class="lineNum">     134 </span>            : 
<span class="lineNum">     135 </span>            : }  // namespace caffe
<span class="lineNum">     136 </span>            : 
<span class="lineNum">     137 </span>            : #endif  // CAFFE_SOLVER_FACTORY_H_
</pre>
      </td>
    </tr>
  </table>
  <br>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="ruler"><img src="../../glass.png" width=3 height=3 alt=""></td></tr>
    <tr><td class="versionInfo">Generated by: <a href="http://ltp.sourceforge.net/coverage/lcov.php" target="_parent">LCOV version 1.12</a></td></tr>
  </table>
  <br>

</body>
</html>
